package com.doucat.support.fast;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.doucat.support.component.fragment.base.ToolbarFragment;
import com.doucat.support.component.tab.VpAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.tonly.doucatlibrary.R;

import java.util.ArrayList;
import java.util.List;

public class TabLayoutFragment extends ToolbarFragment {
    private List<Fragment> fragments;
    private ViewPager2 viewPager2;
    private VpAdapter vpAdapter;
    private List<String> tabTexts;
    private List<Integer> tabDrawables;
    public TabLayout tabLayout;
    private TabLayoutMediator mediator;
    private ViewPager2.OnPageChangeCallback onPageChangeCallback=new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                View tabView = tab.getCustomView();
                TextView tvTabText = tabView.findViewById(R.id.tvTabText);
                ImageView imageTabIcon = tabView.findViewById(R.id.imageTabIcon);
                if (tab.getPosition() == position) {
                    tvTabText.setSelected(true);
                    imageTabIcon.setSelected(true);
                    setTitle(tabTexts.get(position));
                } else {
                    tvTabText.setSelected(false);
                    imageTabIcon.setSelected(false);
                }
            }
        }
    };

    @Override
    public void onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onRootViewCreate(inflater,container,savedInstanceState);
        setTitleFrameLayout(R.layout.fragment_tablayout);
        tabTexts=new ArrayList<>();
        tabDrawables=new ArrayList<>();
        fragments=new ArrayList<>();

        viewPager2 = findViewById(R.id.vp2);
        tabLayout=findViewById(R.id.tabLayout);
        vpAdapter=new VpAdapter(getActivity(),fragments);
        viewPager2.setAdapter(vpAdapter);
        viewPager2.registerOnPageChangeCallback(onPageChangeCallback);

        mediator=new TabLayoutMediator(tabLayout, viewPager2, true,false,new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(TabLayout.Tab tab, int position) {
                View tabView = LayoutInflater.from(getContext()).inflate(R.layout.item_tablayout,null);
                TextView tvTabText = tabView.findViewById(R.id.tvTabText);
                ImageView imageTabIcon = tabView.findViewById(R.id.imageTabIcon);
                ViewGroup groupRead =tabView.findViewById(R.id.groupRead);
                if(position==0){
                    groupRead.setVisibility(View.VISIBLE);
                }else {
                    groupRead.setVisibility(View.GONE);
                }
                tvTabText.setText(tabTexts.get(position));
                imageTabIcon.setImageDrawable(getContext().getDrawable(tabDrawables.get(position)));
                tab.setCustomView(tabView);
            }
        });
        //要执行这一句才是真正将两者绑定起来
        mediator.attach();
        runOnThread(new Runnable() {
            @Override
            public void run() {
//                fragments.add(new MessageFragment());
//                fragments.add(new WorkFragment());
//                fragments.add(new SummarizeFragment());
//                fragments.add(new AccountFragment());
                loadingTabLayoutFragment();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        vpAdapter.notifyDataSetChanged();
                        setTitle(tabTexts.get(0));
                    }
                });
            }
        });
    }

    public void loadingTabLayoutFragment(){

    }

    public void addTab(String title,int res, Fragment fragment){
        tabTexts.add(title);
        tabDrawables.add(res);
        fragments.add(fragment);
    }

    public void setUnReadMessageCount(int count){
        TabLayout.Tab tab = tabLayout.getTabAt(0);
        if(tab!=null){
            View tabView = tab.getCustomView();
            ViewGroup groupRead =tabView.findViewById(R.id.groupRead);
            ImageView imageRed =tabView.findViewById(R.id.imageRed);
            TextView tvUnReadCount =tabView.findViewById(R.id.tvUnReadCount);
            if(count==0l){
                groupRead.setVisibility(View.GONE);
            }else{
                imageRed.setBackgroundResource(R.drawable.unread);
                groupRead.setVisibility(View.VISIBLE);
                tvUnReadCount.setText(count+"");
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mediator.detach();
        viewPager2.unregisterOnPageChangeCallback(onPageChangeCallback);
    }
}